﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmEditProject : FrmBaseEditForm
    {
        public FrmEditProject(Lists.Project project)
        {
            InitializeComponent();
            if (project == null)
            {
                _EditState = EditState.CreatingNew;
                bindingSource.AddNew();
                this.Text = "New Project";
            }
            else
            {
                _EditState = EditState.Modifying;
                bindingSource.Add(DB.Projects.FirstOrDefault(p => p.ProjectID == project.ProjectID));
                this.Text = "Edit Project";
            }
            CompanyIDLookUpEdit.Properties.DataSource = DB.Companies.ToList();
        }

        protected override void CheckFullInfo()
        {
            Lists.Project project = (Lists.Project)bindingSource.Current;
            if (string.IsNullOrEmpty(project.ProjectNo))
                throw new Exception("Project ID not allow null");
            if (string.IsNullOrEmpty(project.ProjectName))
                throw new Exception("Project name not allow null");
        }

        protected override void SaveAndClose()
        {
            Lists.Project project = (Lists.Project)bindingSource.Current;
            if (_EditState == EditState.CreatingNew)
                DB.Projects.InsertOnSubmit(project);
            DB.SubmitChanges();
            this.Close();
        }
    }
}
